package block

import (
	"bytes"
	"encoding/gob"
	"ChainCodeProject/transaction"
	pow2 "ChainCodeProject/Pow"
	"time"
)

/**
 * @author: 清秋
 * @email: 15770670622
 * @datetime: 13:49 2022/3/16
 * @Description:
 */

type Block struct {
	TimeStamp int64 //创建区块时的时间戳
	PrevHash  []byte
	//Data      []byte
	Txs []transaction.Transaction //交易结构体
	Nonce     int64 //随机数
	Hash      []byte
}

func (block *Block) GetTimeStamp() int64 {
	return block.TimeStamp
}
func (block *Block) GetPrevHash() []byte {
	return block.PrevHash
}
func (block *Block) GetData() []transaction.Transaction {
	return block.Txs
}

/*
创建区块
*/
func CreatBlock(txs []transaction.Transaction, prevHash []byte) *Block {
	block := Block{
		TimeStamp: time.Now().Unix(),
		PrevHash:  prevHash,
		Txs:txs,
	}
	//实现了BlockInterface的一个实例
	pow := pow2.NewPow(&block)
	nonce, hash := pow.Run()
	block.Hash = hash
	block.Nonce = nonce
	return &block
}

//序列化：把block结构体类型转成[]byte
func (block *Block) Serialize() ([]byte, error) {
	var result bytes.Buffer
	en := gob.NewEncoder(&result)
	err := en.Encode(block)
	if err != nil {
		return nil, err
	}
	return result.Bytes(), nil
}

//反序列化：把[]byte转成block结构体
func DeSerialize(data []byte) (*Block, error) {
	//var result bytes.Buffer
	reader := bytes.NewReader(data)
	de := gob.NewDecoder(reader)
	var block *Block
	err := de.Decode(&block)
	if err != nil {
		return nil, err
	}
	return block, nil
}

func CreatGenesis(tx transaction.Transaction) *Block {
	return CreatBlock([]transaction.Transaction{tx}, nil)
}
